package osx.mantianx.publicity;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author starrysky
 * @title: PoiPublicity
 * @projectName Excel_Poi_EasyExcel
 * @description: TODO
 * @date 2021/2/411:58
 */
public class PoiPublicity {
    public static void main(String[] args) {
        FileInputStream stream = null;
        XSSFWorkbook workbook = null;
        try {
            stream = new FileInputStream("excel.xlsx");
            workbook = new XSSFWorkbook(stream);

            Sheet sheetAt = workbook.getSheetAt(0);
            Row row = sheetAt.getRow(4);
            Cell cell = row.getCell(1);

            FormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook);
            /**
             * 获取单元格的属性是否为公式，枚举！
             */
            CellType cellTypeEnum = cell.getCellTypeEnum();
            switch (cellTypeEnum) {
                case FORMULA:
                    String cellFormula = cell.getCellFormula();
                    System.out.println(cellFormula);

                    /**
                     * 计算
                     */
                    CellValue evaluate = evaluator.evaluate(cell);
                    String value = evaluate.formatAsString();
                    System.out.println(value);
                default:
                    break;

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    if (stream != null & workbook != null) {
                        stream.close();
                        workbook.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
